package nio.channel.filechannel.write;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class WriteDemo {

    public static void main(String[] args) throws IOException, InterruptedException {
        test3();
    }

    private static FileOutputStream fosRef;
    private static FileChannel fileChannel;

    private static void test3() throws IOException, InterruptedException {

        fosRef = new FileOutputStream(new File("c:\\abc\\a.txt"));
        fileChannel = fosRef.getChannel();
        for (int i = 0; i < 10; i++) {
            Thread thread1 = new Thread() {
                @Override
                public void run() {
                    try {
                        ByteBuffer buffer = ByteBuffer. wrap("abcde\r\n".getBytes());
                        fileChannel.write(buffer);
                    } catch (IOException e) {
                        e. printStackTrace();
                    }
                }
            };
            Thread thread2 = new Thread() {
                @Override
                public void run() {
                    try {
                        ByteBuffer buffer = ByteBuffer.wrap("我是中国人\r\n". getBytes());
                        fileChannel.write(buffer);
                    } catch (IOException e) {
                        e. printStackTrace();
                    }
                }
            };
            thread1.start();
            thread2.start();
        }
        Thread.sleep(3000);
        fileChannel.close();
        fosRef.close();

    }

    private static void test2() throws IOException {

        FileOutputStream fosRef = new FileOutputStream(new File("c:\\abc\\a.txt"));
        FileChannel fileChannel = fosRef.getChannel();
        try {
            ByteBuffer buffer1 = ByteBuffer.wrap("abcde".getBytes());
            ByteBuffer buffer2 = ByteBuffer.wrap("12345".getBytes());
            fileChannel.write(buffer1);
            buffer2.position(1);
            buffer2.limit(3);
            fileChannel.position(2);
            fileChannel.write(buffer2); // ab23e
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileChannel.close();
        fosRef.close();

    }

    private static void test1() throws IOException {

        FileOutputStream fosRef = new FileOutputStream(new File("c:\\abc\\a.txt"));
        FileChannel fileChannel = fosRef.getChannel();
        try {
            ByteBuffer buffer = ByteBuffer.wrap("abcde".getBytes());
            System.out.println("A fileChannel.position()=" + fileChannel.position()); // A fileChannel.position()=0
            System.out.println("write() 1 返回值：" + fileChannel.write(buffer)); // write() 1 返回值：5
            System.out.println("B fileChannel.position()=" + fileChannel.position()); // B fileChannel.position()=5
            fileChannel.position(2);
            buffer.rewind();
            // 注意： 还原 buffer 的 position 为 0
            // 然后在当前位置 position 中再进行写入
            System.out.println("write() 2 返回值：" + fileChannel.write(buffer)); // write() 2 返回值：5
            System.out.println("C fileChannel. position()=" + fileChannel.position()); // C fileChannel. position()=7
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileChannel.close();
        fosRef.close();

    }

}
